package com.rich.web.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.management.RuntimeErrorException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rich.domain.Category;
import com.rich.service.CategoryService;
import com.rich.service.impl.CategoryServiceImpl;
import com.rich.utils.BeanFactory;
import com.rich.utils.UUIDUtils;

/**
 * Servlet implementation class AdminCategoryServlet
 */
@WebServlet("/adminCategory")
public class AdminCategoryServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	CategoryService cs = (CategoryService) BeanFactory.getBean("CategoryService");
	/**
	 * 后台展示分类树
	 * @param request
	 * @param response
	 * @return
	 */
	public String findAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			//调用service获取所有分类
			List<Category> cList = cs.findList();
			//将返回值放入域中，并请求转发
			request.setAttribute("cList", cList);
			return "/admin/category/list.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
	
	/**
	 * 跳转到添加分类页面
	 */
	public String addUI(HttpServletRequest request, HttpServletResponse response) {
		return "/admin/category/add.jsp";
	}
	
	/**
	 *保存分类信息 
	 */
	public String save(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取分类名字
			String cName = request.getParameter("cName");
			//封装category对象
			Category category = new Category();
			category.setcID(UUIDUtils.getId());
			category.setcName(cName);
			//调用service进行保存操作
			cs.save(category);
			//重定向
			response.sendRedirect(request.getContextPath()+"/adminCategory?method=findAll");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	/**
	 * 跳转到修改分类页面
	 */
	public String updateUI(HttpServletRequest request, HttpServletResponse response) {
		try {
			String cid = request.getParameter("c");
			request.setAttribute("cid", cid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "/admin/category/edit.jsp";
	}
	
	/**
	 * 修改分类信息
	 * @param request
	 * @param response
	 * @return
	 */
	public String update(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取要修改的cid
			String cid = request.getParameter("cid");
			String cName = request.getParameter("cName");
			//调用service
			cs.update(cid,cName);
			//重定向
			response.sendRedirect(request.getContextPath()+"/adminCategory?method=findAll");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		return null;
	}
	
	/**
	 * 删除某个分类信息
	 * @param request
	 * @param response
	 * @return
	 */
	public String remove(HttpServletRequest request, HttpServletResponse response) {
		try {
			//获取要删除的cid
			String cid = request.getParameter("cid");
			//删除
			cs.remove(cid);
			//重定向
			response.sendRedirect(request.getContextPath()+"/adminCategory?method=findAll");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
